<?php
namespace Admin\Controller;
use Admin\Controller\CommonController;
use Home\Aip\Store;

class StoreController extends CommonController {
	protected $table = 'store';//店铺表
	protected $member = 'user';//会员表
	protected $integral = 'integral';//会员钱包表
	protected $st_class = 'store_class';//店铺分类表
	protected $st_grade = 'store_grade';//店铺等级表
	protected $st_join = 'store_joinin';//店铺申请表
	protected $gc_class = 'goods_class';//商品分类表
	protected $tb_adv = 'adv';//广告表
	protected $tb_prom = 'promotion';//促销表
	protected $tb_goods = 'product';//产品表
	protected $tb_xianshi = 'p_xianshi';//限时折扣活动表
	protected $tb_g_xianshi = 'p_xianshi_goods';//限时折扣商品表
	protected $tb_xianshi_st = 'p_xianshi_store';//店铺参与的活动表
	
	/*
	*
	*店铺列表
	*/	
    public function index(){
		
		if (!$_GET['p']) {
			$_GET['p'] = 1;
		}			
		$cache = 'store'.$_GET['p'];
		//_Refresh(1,$cache);			
		//搜索
		if ($_GET['grade_id'] || $_GET['member_name'] || $_GET['store_type'] || $_GET['seller_name']) {
			
			unset($arr_data);
			
			if ($_GET ['grade_id']>=1) {				
				$arr_data ['grade_id']  = $_GET ['grade_id'] ;
				$str1 = ' and grade_id=%d';
				$da_str1 = ','.$arr_data ['grade_id'] ;				
			} else {
				$str1 = '';
				$da_str1 = '' ;	
			}
			
			if ($_GET ['store_type']) {	
			
				if ($_GET ['store_type']=='open') {
					//开启
					$arr_data ['store_state']  = 1;
					$arr_name  = 'store_state';
					$type_s = 1;
				} elseif ($_GET ['store_type']=='close') {
					//关闭
					$arr_data ['store_state']  = 0;
					$arr_name  = 'store_state';
					$type_s = 1;
				} elseif ($_GET ['store_type']=='expire') {
					//即将到期,提前三个月显示
					$arr_data ['store_state']  = mktime(0,0,0,date('m')+3) ;					
					$arr_name  = 'store_end_time';
					$type_s = 2;
				} elseif ($_GET ['store_type']=='expired') {
					//已到期
					$arr_data ['store_state']  = time();
					$arr_name  = 'store_end_time';
					$type_s = 3;
				}			
				if ($type_s==1) {
					$str2  = ' and '.$arr_name.'=%d';
					$da_str2 = ','.$arr_data ['store_state'] ;						
				} elseif ($type_s==2) {
					$str2  = ' and '.$arr_name.'<=%d and '.$arr_name.'>%d';
					$da_str2 = ','.$arr_data ['store_state'].','.time() ;					
				} elseif ($type_s==3) {
					$str2  = ' and '.$arr_name.'<=%d ';
					$da_str2 = ','.$arr_data ['store_state'] ;					
				}
				
			} else {
				$str2  = '';
				$da_str2 = '' ;
			}
			
			if ($_GET ['member_name']) {								
				$arr_data ['member_name']  = $_GET ['member_name'] ;
				$str3  = ' and member_name="%s"';
				$da_str3 = ','.$_GET ['member_name'] ;
			} else {
				$str3  = '' ;
				$da_str3 = '' ;
			}
			
			if ($_GET ['seller_name']) {
				$arr_data ['seller_name']  = $_GET ['seller_name'] ;
				$str4  = ' and seller_name="%s"';
				$da_str4 = ','.$_GET ['seller_name'] ;
			} else {
				$str4  = '' ;
				$da_str4 = '' ;
			}
			
			$da_str = '0,0'.$da_str1.$da_str2.$da_str3.$da_str4 ;			
			$str = "store_id>%d and is_own_shop=%d".$str1.$str2.$str3.$str4;			
			$data = explode(',',$da_str);
			//dump($str);
			//dump($data);die;
		    //强制刷新
		   _Refresh(1,$cache);		   
		} else {
			$str = "store_id>%d and is_own_shop=%d";
			$data = array(0,0);			
		}

		if ($_GET['store_join']) {
			//开店申请
			$arr = $this -> store_join();
			//dump($list);die;				
			$this->assign('page',$arr['show']);// 赋值分页输出
			$this->assign('list',$arr['list']);
		    $this->display('store_join');			
		} else if ($_GET['member_id']) {
			//开店申请查看、审核
			unset($arr);		
			$arr = $this -> store_view();			
			$this->assign('store_class',$arr['store_class']);
			$this->assign('joinin_state',$arr['list']['joinin_state']);
			$this->assign('join',$arr['list']);
		    $this->display('store_view');			
		} else {
			//首页
			$num = 15;
			$order = 'store_id desc';
			//分页
			$show = Page($this->table,$str,$data,$num);		
			//强制刷新
			_Refresh($_GET['clear'],$cache);	
			//关联查询
			$list=_getList($this->table,$str,$data,$cache,$order,$_GET['p'],$num);
			//dump();die;
			//店铺到期自动关闭
			foreach ($list as $key => $value) {
				if ($value['store_end_time']<=time()) {				
					$where ['store_id'] = $value ['store_id'] ;
					$data2 ['store_state'] = 0;
					$edit = _getEdit($this->table,$where,'',$data2,$cache) ;
				}			
			}
			
			$this->assign('page',$show);// 赋值分页输出
			$this->assign('list',$list);
		    $this->display();
			
		} 
    }
	
	/*
	*
	*开店申请
	*/
	public function store_join(){
		
		$str = "member_id>%d";
		$data = array(0);			
		$cache = '';
		$num = 15;
		$order = 'member_id desc';
		//分页
		$show = Page($this->st_join,$str,$data,$num);			
		//关联查询
		$list = _getList($this->st_join,$str,$data,$cache,$order,$_GET['p'],$num);
		$arr['list'] = $list;
		$arr['show'] = $show;
		
		return $arr ;
	}

	/*
	*
	*开店申请审核查看页面
	*/
	public function store_view(){
		
		$where['member_id'] = $_GET['member_id'];
				
		//关联查询
		$list = _getOne($this->st_join,$where);	
		
		//主营商品
		$stc = new Store();				
		$store_class = $stc ->StoreClass();
		//dump($store_class);die;
		$arr['list'] = $list;
		$arr['store_class'] = $store_class ;		
		return $arr ;
	}

	/*
	*
	*开店申请审核操作
	*/
	public function edit_stjonin(){
		//dump(11);die;
		
		if (IS_POST) {
			
			$type = I('post.state') ;
			switch ($type) {
				case '20';
					//审核成功					 
					 $error = '';
					 $res = 1;
				break;
				case '30';
					//审核失败
					$error = '' ;
					$res = 2;
					$msg = I('post.msg') ;					
				break;
				case '31';
					//缴费审核失败
					$error = '';
					$res = 3;
					$msg = I('post.msg') ;
				break;	
				case '40';
					//审核通过开店
					$error = '';
					$res = 4;
					$msg = '店铺开通成功';
				break;				
			}
			
			if ($res !== 1) {
				if (!$msg) {
					$error = '请填写拒绝原因！';
				}							
				$data ['joinin_message'] = $msg ;
			}
			
			$data ['joinin_state'] = $type ;
			
			$where ['member_id'] = I('post.id');
			$find_one = _getOne($this->st_join,$where);
			
			if (!$find_one) {
				$error = '会员不存在';
			} else {
				if ($res && !$error) {
					$uid = $find_one['member_id'] ;	
					
					userInfo_cache($uid,'st_joinin_data',1) ;						
					userInfo_cache($uid,'shop_store',1) ;
					
					$edit =  _getEdit($this->st_join,$where,'',$data);	
					
					unset($where);
					unset($data);
					unset($uid);
				}
					
				if ($edit) {
					//审核通过开店
					if ($res==4) {
						//清除session
						$uid = $find_one['member_id'] ;						
						userInfo_cache($uid,'st_joinin_data',1) ;
						
						$sss = userInfo_cache($uid,'shop_store',1) ;
						
						unset($uid);
											
						//添加店铺
						$data ['store_name'] = $find_one ['store_name'] ;//店铺名称
						$data ['grade_id'] = $find_one ['sg_id'] ;//店铺等级
						$data ['member_id'] = $find_one ['member_id'] ;
						$data ['member_name'] = $find_one ['member_name'] ;
						$data ['seller_name'] = $find_one ['seller_name'] ;//店主卖家用户名
						$data ['sc_id'] = $find_one ['sc_id'] ;//店铺分类
						$data ['store_company_name'] = $find_one ['company_name'] ;//店铺公司名称
						$data ['province_id'] = $find_one ['company_province_id'] ;//店铺所在省份ID
						$data ['area_info'] = $find_one ['company_address'] ;//地区内容
						$data ['store_address'] = $find_one ['company_address_detail'] ;//详细地区
						$data ['store_state'] = 1 ;//店铺状态
						$data ['store_time'] = time() ;//店铺时间
						$data ['store_end_time'] = strtotime('+'.$find_one ['joinin_year'].' year') ;//店铺关闭时间
						$data ['store_phone'] = $find_one ['company_phone'] ;//商家电话
						//主营商品
						$stc = new Store();				
						//$store_class = $stc ->StoreClass($find_one);
						//$data ['store_zy'] = strColation($store_class) ;//主营商品
						$data ['live_store_name'] = $find_one ['store_name'] ;//商铺名称
						$data ['store_baozh'] = 0 ;//保证服务开关
						$data ['store_baozhopen'] = 0 ;//保证金显示开关
						$data ['store_baozhrmb'] = $find_one ['sc_bail'] ;//保证金金额
						
						$add = _getAdd($this->table,$data);	
						//$this->ajaxReturn(array('info'=>$add));	
						if ($add) {							
							unset($data);
							$error = 1;
						}
					} else {
						$error = 1;
					}
				} else if (!$edit && !$error) {
					$error = '操作失败';
				}
				unset($res);	
			}
						
			$this->ajaxReturn(array('info'=>$error));	
		}
	}
	
	/*
	*新增店铺
	*
	*/
    public function addStore(){
	
		if (IS_POST) {
			
			I('post.store_name')?$data['store_name'] = I('post.store_name'):$this->error('店铺名称不能为空！','index');			
			//会员非商城密码
			//I('post.storepassword')?$user['password'] = MD5(I('post.storepassword')):$user['password'] = MD5('123456');			
			//会员商城密码
			I('post.storepassword')?$user['password'] = _enPasswd(MD5(I('post.storepassword'))):$user['password'] = _enPasswd(MD5('123456'));	
			
			I('post.member_name')?$data['member_name'] = I('post.member_name'):$this->error('店主账号不能为空！','index');			
			I('post.seller_name')?$data['seller_name'] = I('post.seller_name'):$this->error('卖家账号不能为空！','index');						
			$data['store_state'] = 1;//店铺状态
			$data['store_time'] = time();//注册时间
			$data['store_end_time'] = 0;//结束时间默认为无限期
			$data['is_own_shop'] = 1;//是否自营店铺 1是 0否
			$data['bind_all_gc'] = 1;//是否绑定全部分类 0否1是
			
			$where_1 ['store_name'] = $data['store_name'];
			$find_one = _getOne($this->table,$where_1);
			if ($find_one) {
				$this->error('店铺名称已存在！','index');
			}			
			$where_2 ['member_name'] = $data['member_name'];
			$find_tow = _getOne($this->table,$where_2) ;
			if ($find_tow) {
				$this->error('店主账号已存在！','index');
			}			
			$where_3 ['seller_name'] = $data['seller_name'];
			$find_three = _getOne($this->table,$where_3) ;
			if ($find_three) {
				$this->error('卖家账号已存在！','index');
			}
			
			$where_4 ['username'] = $data['member_name'];
			$find_four = _getOne($this->member,$where_4);
			if ($find_four) {
				//$this->error('会员账号已存在，管理员新增店铺不能用其他会员的账号！','index',10);
				$st_add = 1;
			}
			
			if (!$_GET['p']) {
				$_GET['p'] = 1;
			}
			$cache = 'myuser'.$_GET['p'];
			$cache2 = 'store_'.$_GET['p'];
			$user['username'] = $data['member_name'];	
			if ($st_add) {
				$add = $find_four['id'];
				unset($find_four);
			} else {
				$add = _getAdd($this->member,$user,$cache);
			}
						
			if ($add) {	
				if (!$st_add) {
					$data2['userid'] = $add;				
					$add2 = _getAdd($this->integral,$data2);						
				}			
				$data['member_id'] = $add;
				$add3 = _getAdd($this->table,$data,$cache2);
			}
			//var_dump($add );die;						
			if ($add3) {
				unset($add);
				unset($user);
				unset($data);	
				unset($data2);
				$this->success('新增店铺成功','self');				
			} else {
				$this->error('新增店铺失败！','self');
			}			
		}
    }
	/*
	*店铺分类
	*/	
    public function group(){
		
		if (!$_GET['p']) {
			$_GET['p'] = 1;
		}			
		$cache = '';		
			
		$str = "sc_id>%d";
		$data = array(0);		
		$num = 15;
		$order = 'sc_sort desc';
		//分页
		$show = Page($this->st_class,$str,$data,$num);		
	
		//关联查询
		$list=_getList($this->st_class,$str,$data,$cache,$order,$_GET['p'],$num);		
	
		//dump($list);die;				
		$this->assign('page',$show);// 赋值分页输出
		$this->assign('list',$list);
        $this->display();
    }
		
	/*
	*添加/修改 
	*店铺分类
	*/
    public function addSt(){
	
		if (IS_POST) {
			
			$type = I('post.type');
			switch ($type) {
				case 'class_add':
					$url = 'group';
					I('post.sc_name') ? $data['sc_name'] = I('post.sc_name'):$this->error('分类名称不能为空！',$url);	
					I('post.sc_bail') ? $data['sc_bail'] = I('post.sc_bail'):$data['sc_bail'] =0;		
					I('post.sc_sort') ? $data['sc_sort'] = I('post.sc_sort'):$data['sc_sort'] = 255;
					$res = 1 ;
					$massege = '添加分类成功!' ;
					$error = '添加分类失败！' ;
					$table = $this->st_class ;
					$where_1 ['sc_name'] = I('post.sc_name') ;
					$error2 = '分类已存在！' ;
				break;
				case 'class_edit':
					$url = 'group';
					 $where = array('sc_id'=>I('post.sc_id'));
					I('post.sc_name') ? $data['sc_name'] = I('post.sc_name'):$this->error('分类名称不能为空！',$url);	
					I('post.sc_bail') && $data['sc_bail'] = I('post.sc_bail') ;		
					I('post.sc_sort') && $data['sc_sort'] = I('post.sc_sort') ;
				     $res = 2 ;
					 $massege = '修改分类成功！' ;
					 $error = '修改分类失败！' ;					 
					 $table = $this->st_class ;
					 $where_1 ['sc_name'] = I('post.sc_name') ;
					 $where_1 ['sc_id'] = array('neq',I('post.sc_id')) ;
					 $error2 = '分类已存在！' ;
				break;	
				case 'grade_add':
					$url = 'grade';
					I('post.sg_name')?$data['sg_name'] = I('post.sg_name'):$this->error('等级名称不能为空！',$url);
					I('post.sg_sort') ? $data['sg_sort'] = I('post.sg_sort') : $data['sg_sort'] = 0;						
					I('post.sg_goods_limit') && $data['sg_goods_limit'] = I('post.sg_goods_limit');	
					I('post.sg_album_limit') && $data['sg_album_limit'] = I('post.sg_album_limit') ;		
					I('post.sg_price') ? $data['sg_price'] = I('post.sg_price') : $data['sg_price'] = 0 ;
				     $res = 3 ;
					 $massege = '添加等级成功！' ;
					 $error = '添加等级失败！' ;					
					 $table = $this->st_grade ;
					 $where_1 ['sg_name'] = I('post.sg_name') ;
					 $error2 = '等级名称已存在！' ;
				break;
				case 'grade_edit':
					$where = array('sg_id'=>I('post.sg_id'));
					$url = 'grade';
					I('post.sg_name')?$data['sg_name'] = I('post.sg_name'):$this->error('等级名称不能为空！',$url);	
					I('post.sg_sort') && $data['sg_sort'] = I('post.sg_sort') ;
					I('post.sg_goods_limit') && $data['sg_goods_limit'] = I('post.sg_goods_limit');	
					I('post.sg_album_limit') && $data['sg_album_limit'] = I('post.sg_album_limit') ;		
					I('post.sg_price') && $data['sg_price'] = I('post.sg_price') ;
				     $res = 4 ;
					 $massege = '修改等级成功！' ;
					 $error = '修改等级失败！' ;					 
					 $table = $this->st_grade ;
					 $where_1 ['sg_name'] = I('post.sg_name') ;
					 $where_1 ['sg_id'] = array('neq',I('post.sg_id')) ;
					 $error2 = '等级名称已存在！' ;
				break;					
			}
			
			if ($where_1) {				
				$find_one = _getOne($table,$where_1);
				if ($find_one) {
					$this->error($error2,$url);
				}				
			}
			
			if ($res==1 || $res==3) {
				$change = _getAdd($table,$data);
			} else if ($res==2 || $res==4) {
				$change =  _getEdit($table,$where,'',$data);	
			}			
				
			unset($data);
			unset($res);
			unset($type);	
			unset($where);
			unset($table);			
			if ($change) {
				unset($change);
				$this->success($massege,$url);				
			} else {
				$this->error($error,$url);
			}			
		}
    }
	

	/*
	*店铺等级
	*/	
    public function grade(){
		
		if (!$_GET['p']) {
			$_GET['p'] = 1;
		}			
		$cache = '';		
			
		$str = "sg_id>%d";
		$data = array(0);		
		$num = 15;
		$order = 'sg_id desc';
		//分页
		$show = Page($this->st_grade,$str,$data,$num);		
	
		//关联查询
		$list=_getList($this->st_grade,$str,$data,$cache,$order,$_GET['p'],$num);		
	
		//dump($list);die;				
		$this->assign('page',$show);// 赋值分页输出
		$this->assign('list',$list);
        $this->display();
    }
	
	/*
	*数据删除
	*$type=1为店铺分类表 $type=2为店铺等级表 $type=3为店铺表 $type=4为商品分类表
	*/
    public function del(){
	
		if (IS_POST) {
			$str = I('post.str') ;
			$str = $str."=%d" ;
			$data = array(I('post.id'));
			$massege = '删除成功！' ;
			$error = '删除失败！' ;	
			$type = I('post.table');
			$where = array($str=>I('post.id'));
			
			switch ($type) {
				case '1':
				$table = $this->st_class;
				break;
				case '2':
				$table = $this->st_grade;
				break;
				case '3':
					$res = 3 ;
					$table = $this->table;
					$gcOne = _getOne($table,$where);
					//删除店铺所有商品信息
					unset($str3) ;
					unset($data3) ;
					$str3 = "st_id=%d" ;
					$data3 = array(I('post.id'));
						
					$goods_list = _getList($this->tb_goods,$str3,$data3) ;
					
					//if ($goods_list) {
						//$this->ajaxReturn(array('info'=>'该店铺已发布商品，不能删除'));
					//} else {
						foreach($goods_list as $key=>$v) {					
							unset($str3) ;
							unset($str3) ;
							$str3 = "id=%d" ;
							$str3 = array($v['id']);					
							$del3 = _getDel($this->tb_goods,$str3,$str3);					
						}						
					//}
					
					unset($str3) ;
					unset($data3) ;					
					$str3 = "member_id=%d" ;
					$str3 = array($gcOne['member_id']);
					$del2 = _getDel($this->st_join,$str3,$str3);
					
					//删除店铺所有活动
					$p_xianshi = _getList($this->tb_xianshi,array('store_id'=>I('post.id'))) ;
					foreach($p_xianshi as $key=>$v) {										
						$v['xianshi_id'] &&  _getDel($this->tb_xianshi,array('xianshi_id'=>$v['xianshi_id']));					
					}				
					//删除店铺所有活动商品
					$p_g_xianshi = _getList($this->tb_g_xianshi,array('store_id'=>I('post.id'))) ;
					foreach($p_g_xianshi as $key=>$v) {										
						$v['xianshi_id'] &&  _getDel($this->tb_g_xianshi,array('xianshi_goods_id'=>$v['xianshi_goods_id']));					
					}
				break;
				case '4':
					$table = $this->gc_class;
					$res = 4 ;
					$gcOne = _getOne($table,$where);
					$str2 = "sc_name='%s'" ;
					$data2 = array($gcOne['gc_name']);
					$del2 = _getDel($this->st_class,$str2,$data2);	
				break;
				case '5':
				$table = $this->tb_adv ;
				break;
				case '6':
				$table = $this->tb_prom ;
				break;
				case '7':
					$table = $this->tb_xianshi ;
					$res = 7 ;
					unset($str7);
					unset($data7);
					$str7 = "p_id>%d and xs_id=%d";
					$data7 = array(0,I('post.id'));
					//删除促销活动下的所有店铺
					$list = _getList($this->tb_xianshi_st,$str7,$data7) ;
					if ($list) {
						foreach ($list as $key => $v) {		
							_getDel($this->tb_xianshi_st,array('p_id'=>$v['p_id']));					
						}					
					}
					unset($list);
					unset($str7);
					unset($data7);
					$str7 = "xianshi_goods_id>%d and xianshi_id=%d";
					$data7 = array(0,I('post.id'));
					//删除促销活动下的所有产品
					$list = _getList($this->tb_g_xianshi,$str7,$data7) ;
					if ($list) {
						foreach ($list as $key => $v) {		
							_getDel($this->tb_g_xianshi,array('xianshi_goods_id'=>$v['xianshi_goods_id']));					
						}					
					}
				break;
				case '8':
					$table = $this->tb_xianshi_st ;
					unset($list);
					unset($str7);
					unset($data7);
					$str7 = "xianshi_goods_id>%d and store_id=%d";
					$data7 = array(0,I('post.id'));
						//删除促销活动下的所有产品
					$list = _getList($this->tb_g_xianshi,$str7,$data7) ;
					if ($list) {
						foreach ($list as $key => $v) {		
							_getDel($this->tb_g_xianshi,array('xianshi_goods_id'=>$v['xianshi_goods_id']));					
						}					
					}
				break;
				case '9':
					$table = '' ;
					unset($list);
					unset($str7);
					unset($data7);
					$str7 = "xianshi_goods_id>%d and store_id=%d";
					$data7 = array(0,I('post.id'));
						//删除促销活动下的所有产品
					$list = _getList($this->tb_g_xianshi,$str7,$data7) ;
					if ($list) {
						foreach ($list as $key => $v) {		
							_getDel($this->tb_g_xianshi,array('xianshi_goods_id'=>$v['xianshi_goods_id']));					
						}					
					}
				break;
			}						
			if ($table) {
				$del = _getDel($table,$str,$data);	
			}	
						
			unset($str);
			unset($data);
			unset($gcOne);	
			unset($str2);
			unset($data2);
			unset($where);			
			if ($del) {	
				//清楚缓存
				SClear() ;				
				$this->ajaxReturn(array('info'=>$massege));				
			} else {
				$this->ajaxReturn(array('info'=>$error));	
			}			
		}
    }	
	
	/*
	*数据修改
	*$type=1为店铺分类表 $type=2为店铺等级表 $type=3为店铺表 $type=4为商品分类表
	*/
    public function edit(){
	
		if (IS_POST) {
			
			$str = I('post.str')."=%d" ; 
			$data = array(I('post.id')); 
			$type = I('post.table');
			switch ($type) {
				case '1':
					$table = $this->st_class;
				break;
				case '2':
					$table = $this->st_grade;
				break;
				case '3':				
					$table = $this->table;
					$data2 ['store_state'] = I('post.state') ;
					if (I('post.state')=='1') {
						$res = 1;
					} else {
						$res = 2;
					}
				break;
				case '4':
					$table = $this->gc_class;
				break;
				case '7':
					$table = $this->tb_xianshi;
					$data2 ['state'] = I('post.state') ;
					if (I('post.state')=='1') {
						$res = 1;
					} else {
						$res = 2;
					}
				break;
				case '8':
					$table = $this->tb_xianshi_st;
					$data2 ['p_state'] = I('post.state') ;
					if (I('post.state')=='1') {
						$res = 1;
					} elseif (I('post.state')=='2') {
						$res = 2;
					} elseif (I('post.state')=='3') {
						$res = 3;
					}
				break;
			}
			
			$edit = _getEdit($table,$str,$data,$data2);				
			unset($str);
			unset($data);
			unset($data2);			
			if ($edit) {
				//清楚缓存
				SClear() ;
				$this->ajaxReturn(array('info'=>$res));				
			} else {
				$this->ajaxReturn(array('info'=>4));	
			}			
		}
    }	
	
	
	/*
	*数据修改
	*$type=1为店铺分类表 $type=2为店铺等级表 $type=3为店铺表 $type=4为商品分类表
	*/
    public function self(){
		
		//_Refresh(1,$cache);			
		//搜索
		if ($_GET['store_type'] || $_GET['seller_name']) {
			
			unset($arr_data);
			
			if ($_GET ['store_type']) {	
			
				if ($_GET ['store_type']=='open') {
					//开启
					$arr_data ['store_state']  = 1;
					$arr_name  = 'store_state';
					$type_s = 1;
				} elseif ($_GET ['store_type']=='close') {
					//关闭
					$arr_data ['store_state']  = 0;
					$arr_name  = 'store_state';
					$type_s = 1;
				}			
				if ($type_s==1) {
					$str2  = ' and '.$arr_name.'=%d';
					$da_str2 = ','.$arr_data ['store_state'] ;						
				}
				
			} else {
				$str2  = '';
				$da_str2 = '' ;
			}
			
			if ($_GET ['seller_name']) {
				$arr_data ['seller_name']  = $_GET ['seller_name'] ;
				$str4  = ' and store_name="%s"';
				$da_str4 = ','.$_GET ['seller_name'] ;
			} else {
				$str4  = '' ;
				$da_str4 = '' ;
			}
			
			$da_str = '0,1'.$da_str2.$da_str4 ;			
			$str = "store_id>%d and is_own_shop=%d".$str2.$str4;			
			$data = explode(',',$da_str);
/* 			dump($str);
			dump($data);die;	 */	   
		} else {
			$str = "store_id>%d and is_own_shop=%d";
			$data = array(0,1);			
		}

		//首页
		$num = 15;
		$order = 'store_id desc';
		//分页
		$show = Page($this->table,$str,$data,$num);		

		//查询
		$list=_getList($this->table,$str,$data,'',$order,$_GET['p'],$num);		
			
		$this->assign('page',$show);// 赋值分页输出
		$this->assign('list',$list);
	    $this->display();
    }	
	
	/*
	* 首页分区
	*/
	public function area() {
		$shop_area = M('shop_area');
		if(IS_POST) {
			$data = I('post.');
			$s['shop_area_name'] = $data['shop_area_name'];
			$s['shop_area_sort'] = $data['shop_area_sort'];
			$s['shop_area_href'] = $data['shop_area_href'];
			$s['shop_area_state'] = $data['shop_area_state'];
			$result = $shop_area -> where('shop_area_id = %d', $data['shop_area_id']) -> save($s);
			if($result === false) {
				$this->error('更新失败', 'area');
			}
		}
		$this->assign('list', $shop_area -> select());
		$this->display();
	}	
	
}